package com.esun.cheetah.view.setting.listener;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;

import android.content.Intent;

import com.esun.cheetah.config.Config;
import com.esun.cheetah.view.setting.XmppManager;
import com.esun.cheetah.view.setting.notice.NotificationIQ;

/**
 * 接收push 服务器发来的通知
 * com.esun.cheetah.view.setting.listener.NotificationPacketListener
 * 
 * @author 任付江 create at 2012-4-25 上午11:13:57
 */
public class NotificationPacketListener implements PacketListener {
	private final XmppManager xmppManager;

	public NotificationPacketListener(XmppManager xmppManager) {
		this.xmppManager = xmppManager;
	}

	@Override
	public void processPacket(Packet packet) {
		if (packet instanceof NotificationIQ) {
			NotificationIQ notification = (NotificationIQ) packet;
			if (notification.getChildElementXML().contains(
					"androidpn:iq:notification")) {
				String notificationId = notification.getId();
				String notificationApiKey = notification.getApiKey();
				String notificationTitle = notification.getTitle();
				String notificationMessage = notification.getMessage();
				String notificationUri = notification.getUri();
				Intent intent = new Intent(Config.ACTION_SHOW_NOTIFICATION);
				intent.putExtra(Config.NOTIFICATION_ID, notificationId);
				intent.putExtra(Config.NOTIFICATION_API_KEY, notificationApiKey);
				intent.putExtra(Config.NOTIFICATION_TITLE, notificationTitle);
				intent.putExtra(Config.NOTIFICATION_MESSAGE,
						notificationMessage);
				intent.putExtra(Config.NOTIFICATION_URI, notificationUri);
				xmppManager.getContext().sendBroadcast(intent);// 接收到push发送广播
			}
		}
	}
}
